package cuiyt.leetcode.dichotomy.demoa;

/**
 * @author cyt
 * @create 2020-09-06 15:09
 */
public class Tb {

    public static void main(String[] args) {
        Tb tb = new Tb();
        System.out.println(tb.mySqrt(8));
    }

    public int mySqrt(int x) {
        int l = 0;
        int r = x / 2 + 1;
        while (l <= r) {
            long mid = l + (r - l) / 2;
            long ans = mid * mid;
            if (ans == x) {
                return (int) mid;
            } else if (ans < x) {
                l = (int) (mid + 1);
            } else {
                r = (int) (mid - 1);
            }
        }
        return r;
    }

}
